﻿using System;
using Invest.DataBaseUpdater.Core;
using InvestmentIntelligence.DB;
using NDesk.Options;

namespace Invest.DataBaseUpdater
{
    class Program
    {
        static void Main(string[] args)
        {
            string runType = null;
            bool defaultDb = false;
            bool showHelp = false;
            string serverName = null;
            string userName = null;
            string password = null;
            string dbName = null;
            string dbLocation = null;

            var optionSet = new OptionSet
                {
                     {"h|?|help", "show this message and exit", v => showHelp = v!=null},
                     {"update-type=","Update DB Type [CREATE] [UPDATE] [INSERTUPDATEDATA]", x => runType = x},
                     {"server-name=","DB Server Name", x => serverName = x},
                     {"user-name=", "DB User Name, don't use if need Integrated Security", x => userName = x},
                     {"password=","DB Password, don't use if need Integrated Security", x => password = x},
                     {"database-name=","Database name", x => dbName = x},
                     {"defaultdb", "Use if need default db parameters",x => defaultDb =x!=null},
                     {"location=", "DataBase file location",x => dbLocation =x},
                };

            optionSet.Parse(args);

            if (showHelp)
            {
                DisplayCommandLineHelp(optionSet);
                return;
            }

            switch (runType)
            {   
                case "CREATE":
                    {
                        ConfigureConnection(defaultDb, serverName, dbName, userName, password, null);
                        using (var processor = new CreateDbProcessor(IIPConnectionString.CreateSqlConnection()))
                        {
                            if (string.IsNullOrWhiteSpace(dbLocation))
                            {
                                processor.Process();
                            }
                            else
                            {
                                processor.Process(dbLocation);
                            }
                            return;
                        }
                    }
                case "UPDATE":
                    {
                        ConfigureConnection(defaultDb, serverName, dbName, userName, password, dbLocation);
                        using (var processor = new UpdateDbProcessor(IIPConnectionString.CreateSqlConnection()))
                        {
                            processor.Process();
                            return;
                        }
                    }
                case "INSERTUPDATEDATA":
                    {
                        ConfigureConnection(defaultDb, serverName, dbName, userName, password, dbLocation);
                        using (var processor = new LogProcessor(IIPConnectionString.CreateSqlConnection()))
                        {
                            processor.InsertUpdateData();
                            return;
                        }
                    }
            }
        }


        private static void DisplayCommandLineHelp(OptionSet options)
        {
            Console.WriteLine("PARAMETERS:");
            Console.WriteLine(" --update-type with parameter CREATE UPDATE INSERTUPDATEDATA");
            Console.WriteLine();
            options.WriteOptionDescriptions(Console.Out);
        }

        private static void ConfigureConnection(bool defaultDb, string serverName, string dbName, string userName, string password, string fileName)
        {
            if (defaultDb)
            {
                IIPConnectionString.ConfigureConnection("IIP_Dev", null, ".");
                return;
            }

            if (serverName != null)
            {
                IIPConnectionString.ConfigureConnection(dbName, fileName, serverName, userName, password, string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password));
            }
        }
    }
}
